from django.contrib.auth.models import AbstractUser
from django.db import models


# Create your models here.

class Company(models.Model):
    company_name = models.CharField(max_length=128)
    company_addr = models.CharField(max_length=128)
    company_code = models.CharField(max_length=4,null=True,unique=True)

    class Meta:
        db_table = 'company'
        verbose_name = "公司"


class UserGroup(models.Model):
    company = models.ForeignKey(Company,on_delete=models.CASCADE)



class User(AbstractUser):
    wechat_id = models.CharField(max_length=64) # openid
    company = models.ForeignKey(Company,on_delete=models.SET_NULL,null=True)
    mobile = models.CharField(max_length=11, unique=True, verbose_name='手机号')
    group = models.ForeignKey(UserGroup,on_delete=models.SET_NULL,null = True)

    class Meta:
        db_table = 'tb_user'
        verbose_name = '用户'
        verbose_name_plural = verbose_name

class WXmini_Auth(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    open_id = models.CharField(unique=True,max_length=255)

    class Meta:
        db_table = 'wx_auth'
        verbose_name = "微信认证"




class PcDevice(models.Model):
    mac_addr = models.CharField(max_length=64)  # mac地址
    ip_addr = models.GenericIPAddressField()    # ip地址
    host_name = models.CharField(max_length = 32)   # host 名称
    company = models.ForeignKey(Company, on_delete=models.CASCADE)  # 公司
    machine_code = models.CharField(max_length=128,null=True,unique=True)


    class Meta:
        db_table = "pc_device"
        verbose_name = "PC设备"


class Process(models.Model):
    process_name = models.CharField(max_length=128,unique=True) # 流程名称
    process_type = models.CharField(max_length=128,unique=False) # RPA类型
    process_path = models.CharField(max_length=256) # 脚本路径
    process_local = models.CharField(max_length=32,null=False,default="127.0.0.1",unique=False) # IP地址
    # process_user = models.ForeignKey(User,on_delete=models.CASCADE,default=1,unique=False) # 所属用户
    process_status = models.CharField(max_length=32,unique=False)
    pcdevice = models.ForeignKey(PcDevice,on_delete=models.CASCADE,default=1,unique=False)
    group = models.ForeignKey(UserGroup,on_delete=models.SET_NULL,null = True)

    # machine_code = models.ForeignKey(PcDevice,to_field='machine_code',on_delete=models.CASCADE,default=0)


    class Meta:
        db_table = "process"
        verbose_name = "流程"


class Job(models.Model):
    process = models.ForeignKey(Process,on_delete=models.CASCADE)
    start_time = models.CharField(max_length=32)
    end_time = models.CharField(max_length=32,null=True)
    status = models.CharField(max_length=32,unique=False)
    celery_key = models.CharField(max_length=128,unique=False,null=True)
    class Meta:
        db_table = 'process_job'
        verbose_name = "任务"

